<template>
  <div class="container">
    <div class="background-animations">
      <div class="cube"></div>
      <div class="cube"></div>
      <div class="cube"></div>
      <div class="cube"></div>
      <div class="cube"></div>
    </div>
    <div class="register-box animate__animated animate__fadeIn animate__faster">
      <div class="header-section">
        <div class="logo-container">
          <img :src="require('../assets/imgs/logo.png')" alt="CA Logo" class="logo-img"/>
        </div>
        <div class="title-container">
          <div class="title">用户注册</div>
          <div class="subtitle">Join Computer Association</div>
        </div>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input 
            prefix-icon="el-icon-user" 
            placeholder="请输入学号" 
            v-model="form.username"
            class="custom-input">
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input 
            prefix-icon="el-icon-mobile-phone"
            placeholder="请输入手机号码" 
            v-model="form.phone"
            class="custom-input">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            prefix-icon="el-icon-lock" 
            placeholder="请输入密码" 
            show-password  
            v-model="form.password"
            class="custom-input">
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            prefix-icon="el-icon-lock" 
            placeholder="请确认密码" 
            show-password  
            v-model="form.confirmPassword"
            class="custom-input">
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <div class="captcha-container">
            <el-input 
              prefix-icon="el-icon-key"
              placeholder="请输入验证码" 
              v-model="form.captcha"
              class="custom-input captcha-input">
            </el-input>
            <div 
              class="captcha-img" 
              @click="refreshCaptcha" 
              v-html="captchaSvg"
            ></div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="register-btn" @click="register">
            <span>注 册</span>
          </el-button>
        </el-form-item>
        <div class="login-link">
          <div></div>
          <div>
            已有账号？请 
            <router-link 
              to="/login" 
              class="login-text"
              @click.native="goToLogin">
              登录
            </router-link>
          </div>
        </div>
      </el-form>
    </div>
    <div class="tech-circles">
      <div class="circle"></div>
      <div class="circle"></div>
      <div class="circle"></div>
    </div>
    <div class="tech-lines">
      <div class="line"></div>
      <div class="line"></div>
      <div class="line"></div>
    </div>
    <div class="footer">
      <div class="copyright">
        广软计算机协会 © 2025
        <a href="http://ca.seig.edu.cn" target="_blank" class="footer-link">ca.seig.edu.cn</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validatePhone = (rule, value, callback) => {
      const phoneRegex = /^1[3-9]\d{9}$/
      if (value === '') {
        callback(new Error('请输入手机号码'))
      } else if (!phoneRegex.test(value)) {
        callback(new Error('请输入正确的手机号码'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: '',
        phone: '',
        captcha: '',
        role: 'USER'
      },
      captchaSvg: '',
      captchaText: '',
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    generateCaptcha() {
      const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
      let result = ''
      for (let i = 0; i < 4; i++) {
        result += chars[Math.floor(Math.random() * chars.length)]
      }
      return result
    },

    generateCaptchaSvg(text) {
      const width = 100
      const height = 40
      
      const randomColor = () => {
        const r = Math.floor(Math.random() * 256)
        const g = Math.floor(Math.random() * 256)
        const b = Math.floor(Math.random() * 256)
        return `rgb(${r},${g},${b})`
      }

      let lines = ''
      for (let i = 0; i < 3; i++) {
        const x1 = Math.random() * width
        const y1 = Math.random() * height
        const x2 = Math.random() * width
        const y2 = Math.random() * height
        lines += `<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" stroke="${randomColor()}" stroke-width="1"/>`
      }

      let dots = ''
      for (let i = 0; i < 30; i++) {
        const x = Math.random() * width
        const y = Math.random() * height
        dots += `<circle cx="${x}" cy="${y}" r="1" fill="${randomColor()}"/>`
      }

      let letters = ''
      for (let i = 0; i < text.length; i++) {
        const x = 20 + i * 20
        const y = 25 + Math.random() * 8
        const rotate = Math.random() * 30 - 15
        const color = randomColor()
        letters += `
          <text x="${x}" y="${y}" 
                fill="${color}" 
                transform="rotate(${rotate} ${x} ${y})"
                style="font-size: 24px; font-family: Arial; font-weight: bold;">
            ${text[i]}
          </text>
        `
      }

      return `
        <svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
          <rect width="100%" height="100%" fill="white"/>
          ${lines}
          ${dots}
          ${letters}
        </svg>
      `
    },

    refreshCaptcha() {
      this.captchaText = this.generateCaptcha()
      this.captchaSvg = this.generateCaptchaSvg(this.captchaText)
      this.form.captcha = ''
    },

    register() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          if (this.form.captcha.toUpperCase() !== this.captchaText) {
            this.$message.error('验证码错误')
            this.refreshCaptcha()
            return
          }
          
          this.$request.post('/register', this.form).then(res => {
            if (res.code === '200') {
              this.$message.success('注册成功，请登录')
              this.$router.push('/login')
            } else {
              this.$message.error(res.msg)
              this.refreshCaptcha()
            }
          })
        }
      })
    },
    goToLogin() {
      this.$router.push('/login')
    }
  },
  mounted() {
    this.refreshCaptcha()
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(135deg, 
    rgba(23, 36, 64, 0.95), 
    rgba(31, 54, 96, 0.95)
  );
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}

.container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 20%, rgba(41, 98, 255, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(0, 200, 255, 0.08) 0%, transparent 50%);
  z-index: 0;
  animation: gradientShift 15s ease infinite;
}

@keyframes gradientBG {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.register-box {
  width: 420px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(20px);
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
  position: relative;
  z-index: 10;
}

.header-section {
  text-align: center;
  margin-bottom: 30px;
  padding: 0 20px;
}

.logo-container {
  text-align: center;
  margin-bottom: 15px;
  position: relative;
  padding: 5px;
  background: transparent;
}

.logo-img {
  width: 180px;
  height: auto;
  object-fit: contain;
  transition: all 0.3s ease;
  filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.1));
}

.logo-img:hover {
  transform: scale(1.02);
  filter: drop-shadow(2px 4px 8px rgba(0, 0, 0, 0.15));
}

.title-container {
  position: relative;
  padding-top: 5px;
}

.title-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 2px;
  background: linear-gradient(90deg, transparent, #ff3333, transparent);
}

.title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
  background: linear-gradient(45deg, #ff3333, #cc0000);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 1px;
}

.subtitle {
  font-size: 13px;
  color: #666;
  letter-spacing: 1px;
  margin-bottom: 25px;
  font-family: 'Arial', sans-serif;
}

.custom-input :deep(.el-input__inner) {
  height: 45px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__inner):focus {
  border-color: #1a2a6c;
  box-shadow: 0 0 0 2px rgba(26, 42, 108, 0.2);
}

.custom-select {
  width: 100%;
}

.custom-select :deep(.el-input__inner) {
  height: 45px;
  border-radius: 8px;
}

.register-btn {
  position: relative;
  z-index: 12;
  width: 100%;
  height: 45px;
  border-radius: 8px;
  font-size: 16px;
  background: linear-gradient(45deg, #ff3333, #cc0000);
  border: none;
  transition: all 0.3s ease;
}

.register-btn:hover {
  background: linear-gradient(45deg, #cc0000, #ff3333);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(26, 42, 108, 0.3);
}

.login-link {
  position: relative;
  z-index: 12;
  display: flex;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
}

.login-text {
  color: #cc0000;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  cursor: pointer;
  padding: 2px 4px;
}

.login-text:hover {
  color: #ff3333;
  text-decoration: underline;
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #1a2a6c;
  border-color: #1a2a6c;
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #1a2a6c;
}

.tech-circles {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.circle {
  position: absolute;
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.circle:nth-child(1) {
  width: 600px;
  height: 600px;
  bottom: -200px;
  right: -200px;
  border-color: rgba(65, 184, 255, 0.1);
  animation: rotate 30s linear infinite;
}

.circle:nth-child(2) {
  width: 400px;
  height: 400px;
  top: -100px;
  left: -100px;
  border-color: rgba(0, 255, 255, 0.1);
  animation: rotate 20s linear infinite reverse;
}

.circle:nth-child(3) {
  width: 200px;
  height: 200px;
  top: 50%;
  left: 50%;
  border-color: rgba(255, 255, 255, 0.1);
  animation: rotate 15s linear infinite;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.tech-lines {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
  opacity: 0.1;
}

.line {
  position: absolute;
  background: linear-gradient(90deg, transparent, #fff, transparent);
  height: 1px;
  width: 100%;
  animation: scan 3s linear infinite;
}

.line:nth-child(1) {
  top: 20%;
  animation-delay: 0s;
}

.line:nth-child(2) {
  top: 50%;
  animation-delay: 1s;
}

.line:nth-child(3) {
  top: 80%;
  animation-delay: 2s;
}

@keyframes scan {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

.background-animations {
  position: fixed;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  z-index: 1;
}

.cube {
  position: absolute;
  width: 60px;
  height: 60px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  animation: floatCube 20s linear infinite;
  transform-style: preserve-3d;
}

.cube:nth-child(1) {
  top: 20%;
  left: 20%;
  animation-delay: 0s;
  animation-duration: 15s;
}

.cube:nth-child(2) {
  top: 60%;
  left: 70%;
  width: 90px;
  height: 90px;
  animation-delay: -2s;
  animation-duration: 25s;
}

.cube:nth-child(3) {
  top: 30%;
  left: 80%;
  width: 40px;
  height: 40px;
  animation-delay: -4s;
  animation-duration: 20s;
}

.cube:nth-child(4) {
  top: 70%;
  left: 30%;
  width: 70px;
  height: 70px;
  animation-delay: -6s;
  animation-duration: 22s;
}

.cube:nth-child(5) {
  top: 40%;
  left: 50%;
  width: 50px;
  height: 50px;
  animation-delay: -8s;
  animation-duration: 18s;
}

@keyframes floatCube {
  0% {
    transform: translateY(0) rotateX(0) rotateY(0) translateZ(0);
    opacity: 1;
  }
  50% {
    transform: translateY(-100vh) rotateX(180deg) rotateY(360deg) translateZ(100px);
    opacity: 0.5;
  }
  100% {
    transform: translateY(-200vh) rotateX(360deg) rotateY(720deg) translateZ(0);
    opacity: 0;
  }
}

.footer {
  position: fixed;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  z-index: 1;
}

.copyright {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
  letter-spacing: 0.5px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.footer-link {
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  margin-left: 5px;
  transition: all 0.3s ease;
  position: relative;
}

.footer-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 1px;
  background: rgba(255, 255, 255, 0.5);
  transform: scaleX(0);
  transition: transform 0.3s ease;
}

.footer-link:hover {
  color: #fff;
}

.footer-link:hover::after {
  transform: scaleX(1);
}

.el-form {
  position: relative;
  z-index: 11;
}

.custom-input {
  position: relative;
  z-index: 12;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-input {
  flex: 1;
}

.captcha-img {
  width: 100px;
  height: 40px;
  cursor: pointer;
  border-radius: 4px;
  overflow: hidden;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.captcha-img:hover {
  opacity: 0.8;
}

:deep(svg) {
  width: 100%;
  height: 100%;
}
</style>